package com.safe.ly_safe.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.safe.ly_safe.R;
import com.safe.ly_safe.base.BaseApplication;
import com.safe.ly_safe.utils.SharedUtils;

import java.lang.ref.WeakReference;


/**
 * @PACKAGE :com.safe.ly_safe.activity
 * @Author : HSF
 * @Time : On 2022/4/25 9:07
 * @Description : WelcomeActivity
 * 启动页
 */
public class WelcomeActivity extends AppCompatActivity {

    private boolean hasLogin = false;

    private static final int GO_LOGIN = 1001;

    private static final int GO_MAIN = 1002;

    private static final int DELLY_TIME = 1000;

    private final WelcomeHandler welcomeHandler = new WelcomeHandler(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加入任务栈
        BaseApplication.getInstance().joinTaskStack(this);
        checkForFirstUp();
    }

    /*检查是否已登录*/
    private void checkForFirstUp() {
        hasLogin = (boolean) SharedUtils.getSharedInfo(this,SharedUtils.KEY_HAS_LOGIN_IN,false);
        if (!hasLogin) {
            welcomeHandler.sendEmptyMessageDelayed(GO_LOGIN,DELLY_TIME);
        } else {
            welcomeHandler.sendEmptyMessageDelayed(GO_MAIN,DELLY_TIME);
        }
    }

    private static class WelcomeHandler extends Handler {

        private final WeakReference<Activity> activityWeakReference;

        WelcomeHandler(Activity activity) {
            this.activityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            WelcomeActivity welcomeActivity = (WelcomeActivity) activityWeakReference.get();
            switch (msg.what){
                case GO_LOGIN:
                    welcomeActivity.goLogin();
                    break;
                case GO_MAIN:
                    welcomeActivity.goMainHome();
                    break;
            }
        }
    }

    private void goLogin(){
        startActivity(new Intent(this,LoginActivity.class));
        finish();
    }

    private void goMainHome(){
        startActivity(new Intent(this,MainActivity.class).putExtra("fromStr","WelcomeActivity"));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
        hasLogin = false;
        welcomeHandler.removeCallbacksAndMessages(null);
    }
}
